package com.share.common.raw;

import com.share.common.util.DataSourceUtil;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author caifan
 * @createTime 2020/10/15
 * @desc:
 */
public class DatasourceHelper {

    public DataSource createDatasource() {
        Map<String, DataSource> dataSourceMap = new HashMap<>();
        DataSource dataSource1 = DataSourceUtil.createDataSource("localhost", "root", "123456", 3306, "demo_ds_0");
        DataSource dataSource2 = DataSourceUtil.createDataSource("demo_ds_1");
        dataSourceMap.put("ds1", dataSource1);
        dataSourceMap.put("ds2",dataSource2);
//        ShardingDataSourceFactory.createDataSource(dataSourceMap);
        // 配置 t_order 表规则
//        ShardingTableRuleConfiguration orderTableRuleConfig = new ShardingTableRuleConfiguration("t_order", "ds${0..1}.t_order${0..1}");
// 配置分库策略
//        orderTableRuleConfig.setDatabaseShardingStrategy(new
//                StandardShardingStrategyConfiguration("user_id", "dbShardingAlgorithm"));
// 配置分表策略
//        orderTableRuleConfig.setTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "tableShardingAlgorithm"));
// 省略配置 t_order_item 表规则...
// ...
// 配置分⽚规则
//        ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
//        shardingRuleConfig.getTables().add(orderTableRuleConfig);
//// 配置分库算法
//        Properties dbShardingAlgorithmrProps = new Properties();
//        dbShardingAlgorithmrProps.setProperty("algorithm-expression", "ds${user_id % 2}");
//        shardingRuleConfig.getShardingAlgorithms().put("dbShardingAlgorithm", new ShardingSphereAlgorithmConfiguration("INLINE", dbShardingAlgorithmrProps));
//        // 配置分表算法
//        Properties tableShardingAlgorithmrProps = new Properties();
//        tableShardingAlgorithmrProps.setProperty("algorithm-expression", "t_order${order_id % 2}");
//            shardingRuleConfig.getShardingAlgorithms().put("tableShardingAlgorithm", new
//                                                           ShardingSphereAlgorithmConfiguration("INLINE", tableShardingAlgorithmrProps));
//        // 创建 ShardingSphereDataSource
//    DataSource dataSource = ShardingSphereDataSourceFactory.
//            createDataSource(dataSourceMap, Collections.singleton((shardingRuleConfig), new Properties());
        return null;

    }

}
